import { http } from "@/utils/http";
import type { FormItemProps } from "@/views/system/menu/utils/types";
import type { ResultTable, IQuery, Status } from "@/hooks/types";
import type { FormItemProps as RoleFormItemProps } from "@/views/system/role/utils/types";
import type { FormItemProps as DeptFormItemProps } from "@/views/system/dept/utils/types";

type Result<T = any> = Array<T>;

export type RoleListParams = {
  name?: string;
  code?: string;
  status?: string;
} & IQuery;

export type RoleListData = {
  id: number;
  name: string;
  code: string;
  status: number;
  remark: string;
  createTime: string;
  updateTime: string;
};

export type RoleMenuData = {
  id: number;
  parentId: number;
  menuType: number;
  title: string;
};

export type RoleMenuIdsParams = {
  roleId: number;
};

export type RoleMenuParams = {
  id: number;
  menuIds: number[];
};

export type DeptListParams = {
  name: string;
  status: Status;
} & IQuery;

export type DeptListData = {
  id: number;
  parentId: number;
  name: string;
  sort: number;
  phone: string;
  principal: string;
  email: string;
  status: number;
  type: number;
  remark: string;
};

export type UserListParams = {
  nickname?: string;
  username?: string;
  phone?: string;
  dept?: number;
  status?: number;
} & IQuery;

export type UserListData = {
  avatar: string;
  username: string;
  nickname: string;
  phone: string;
  email: string;
  sex: number;
  id: number;
  status: number;
  dept: {
    // 部门id
    id: number;
    // 部门名称
    name: string;
  };
  remark: string;
  createTime: string;
};

/** 系统管理-用户管理-获取所有角色列表 */
export const getAllRoleList = () => {
  return http.request<Result>("get", "/list-all-role");
};

/** 系统管理-用户管理-根据userId，获取对应角色id列表（userId：用户id） */
export const getRoleIds = (data?: object) => {
  return http.request<Result>("post", "/list-role-ids", { data });
};

/** 获取系统监控-在线用户列表 */
export const getOnlineLogsList = (data?: object) => {
  return http.request<ResultTable>("post", "/online-logs", { data });
};

/** 获取系统监控-登录日志列表 */
export const getLoginLogsList = (data?: object) => {
  return http.request<ResultTable>("post", "/login-logs", { data });
};

/** 获取系统监控-操作日志列表 */
export const getOperationLogsList = (data?: object) => {
  return http.request<ResultTable>("post", "/operation-logs", { data });
};

/** 获取系统监控-系统日志列表 */
export const getSystemLogsList = (data?: object) => {
  return http.request<ResultTable>("post", "/system-logs", { data });
};

/** 获取系统监控-系统日志-根据 id 查日志详情 */
export const getSystemLogsDetail = (data?: object) => {
  return http.request<Result>("post", "/system-logs-detail", { data });
};

/** 获取系统管理-菜单管理列表 */
export const getMenuList = (data?: object) => {
  return http.request<Result>("get", "/system/menu/list", { params: data });
};

// 删除菜单
export const deleteMenu = (data: { id: number }) => {
  return http.request("delete", "/system/menu/delete", { params: data });
};

// 新增菜单
export const addMenu = (data: Omit<FormItemProps, "higherMenuOptions">) => {
  return http.request("post", "/system/menu/add", { data });
};

// 更新菜单
export const updateMenu = (data: Omit<FormItemProps, "higherMenuOptions">) => {
  return http.request("put", "/system/menu/update", { data });
};

/** 获取系统管理-角色管理列表 */
export const getRoleList = (data?: RoleListParams) => {
  return http.request<ResultTable<RoleListData>>("get", "/system/role/list", {
    params: data
  });
};

/** 获取角色管理-权限-菜单权限 */
export const getRoleMenu = () => {
  return http.request<Result<RoleMenuData>>("get", "/system/role/role-menu");
};

/** 获取角色管理-权限-菜单权限-根据角色 id 查对应菜单 */
export const getRoleMenuIds = (data: RoleMenuIdsParams) => {
  return http.request<Result>("get", "/system/role/list-role-ids", {
    params: data
  });
};

// 新增角色
export const addRole = (data: RoleFormItemProps) => {
  return http.request("post", "/system/role/add", { data });
};

// 修改角色
export const updateRole = (data: RoleFormItemProps) => {
  return http.request("put", "/system/role/update", { data });
};

// 删除角色
export const deleteRole = (data: { id: number }) => {
  return http.request("delete", "/system/role/delete", { params: data });
};

// 设置角色菜单权限/
export const setRoleMenu = (data: RoleMenuParams) => {
  return http.request("put", "system/role/bind/menu", { data });
};

/** 获取系统管理-部门管理列表 */
export const getDeptList = (data?: DeptListParams) => {
  return http.request<ResultTable<DeptListData>>("get", "/system/dept/list", {
    params: data
  });
};

// 新增部门
export const addDept = (data: Omit<DeptFormItemProps, "higherDeptOptions">) => {
  return http.request("post", "/system/dept/add", { data });
};

// 更新部门
export const updateDept = (
  data: Omit<DeptFormItemProps, "higherDeptOptions">
) => {
  return http.request("put", "/system/dept/update", { data });
};

// 删除部门
export const deleteDept = (data: { id: number }) => {
  return http.request("delete", "/system/dept/delete", { params: data });
};

/** 获取系统管理-用户管理列表 */
export const getUserList = (data?: UserListParams) => {
  return http.request<ResultTable<UserListData>>("get", "/user/list", {
    params: data
  });
};
